/*
	ADT - Actionscript Developer's Tool
	Copyright (C) 2010-2011  Damon "Drawm" Perron-Laurin
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program.  If not, see http://www.gnu.org/licenses/.
*/

package com.drawm.adt.debugger {
	import flash.text.TextField;
	import flash.events.Event;
	import flash.display.Sprite;

	/**
	 * @author Drawm
	 */
	public class Console extends Sprite {
		
//		Store values name to show
		private static var _values:Array = [];
//		Define if the manager is on or off
		private static var _debug:Boolean = false;
//		Turn on / off the trace system
		private static var _isTraceEnable:Boolean = false;
//		Maximum number of line showed in the console
		private static var _maxTraceLine:uint = 6;
//		Store msg to trace, each case is a line on the console
		private static var _traceLines:Array = [];
//		The actual TextField for the console
		private var _txt:TextField = new TextField();
		
		public function Console(width:int = 300, height:int = 200){
//			Set the console size
			_txt.width = width;
			_txt.height = height;
			
//			Set style parameter (might add some function to change it later)
			this.alpha = 0.8;
			this.graphics.beginFill(0xBBBBBB,0.5);
			this.graphics.drawRect(0, 0, width, height);
			
			
//			On added to stage event
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		}
		
		public static function trace(msg:String):void{
			_traceLines.push(msg);
			if(_traceLines.length >= _maxTraceLine)
				_traceLines.shift();
		}
		
		public static function addValue(source:*, value:String, type:String = "default"):void{
			_values.push([source, value, type]);
		}
		public static function addArray(source:*, value:uint, type:String = "array"):void{
			_values.push([source, value, type]);
		}
		public static function removeValue(source:*, value:String):void{
			for(var i:String in _values){
				if(_values[i][0] == source && _values[i][1] == value){
					var index : int = _values.indexOf(_values[i]);
					_values.splice(index,index-1);
				}
			}
		}
//		
		private function onAddedToStage(event : Event) : void {
//			Remove the added to stage event listener
			event.target.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			
//			Deactivate mouse event on the console (might add a parameter to make the text selectable)
			this.mouseEnabled = false;
			this.mouseChildren = false;
			
//			Add the TextField to the display list
			_txt.text = "Debuging";
			this.addChild(_txt);
			
//			Start the enterframe
//			(Will be changed soon)
			this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
//		Show everything that as to be in the console
//		Usign an enterframe is bad, I know, I will change it soon
		private function onEnterFrame(event : Event) : void {
			if(_debug || _isTraceEnable){
			
				_txt.text = "";
				var newTxt:String = "";
				
				if(_debug)
					for(var debugTxtIndex:* in _values)
						newTxt +=  _values[debugTxtIndex][0]+"."+_values[debugTxtIndex][1] + " = "+ _values[debugTxtIndex][0][_values[debugTxtIndex][1]]+"\n";
				
				if(_debug && _isTraceEnable)
					newTxt += " \n---------------------------------------------------------------\n ";
				
				if(_isTraceEnable)
					for(var traceTxtIndex:* in _traceLines)
						newTxt +=  _traceLines[traceTxtIndex]+"\n";
				
					
				_txt.text = newTxt;
			}
		}
//		Start the console
		public static function start():void{
			_debug = true;
		}
//		Stop the console
		public static function stop():void{
			_debug = false;
		}
//-------------------------------------------------------------------------------//
//------GETTER / SETTER ---------------------------------------------------------//
//-------------------------------------------------------------------------------//
		public static function set isTraceEnable(value:Boolean):void{
			_isTraceEnable = value;
		}
		public static function set maxTraceLine(value:uint):void{
			_maxTraceLine = value;
		}
		public static function get values():Array{
			return _values;
		}
		
	}
}
